package com.ambow.util;

import java.sql.Connection;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;

@Intercepts(@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }))
public class MySQLPagingInterceptor extends AbstractPagingInterceptor {

	@Override
	protected String getSelectTotalSql(String targetSql) {
		String sql = targetSql.toLowerCase();
		StringBuilder sqlBuilder = new StringBuilder(sql);

		int orderByPos = 0;
		if ((orderByPos = sqlBuilder.lastIndexOf(ORDER_BY)) != -1) {
			sqlBuilder.delete(orderByPos, sqlBuilder.length());
		}
		sqlBuilder.insert(0, "select count(1) as _count from ( ").append(" ) tt_table_name");
		System.out.println(sqlBuilder.toString());
		return sqlBuilder.toString();
	}

	@Override
	protected String getSelectPagingSql(String targetSql, PagingBounds pagingBounds) {
		String sql = targetSql.toLowerCase();
		StringBuilder sqlBuilder = new StringBuilder(sql);

//		sqlBuilder.insert(0, "select * from ( select table_alias.*, rownum mybatis_rowNo from (");
//		sqlBuilder.append(") ");
//		sqlBuilder.append("table_alias where rownum <= " + pagingBounds.getSelectCount()).append(")");
//		sqlBuilder.append("where mybatis_rowNo >= " + (pagingBounds.getOffset() + 1));
		
		sqlBuilder.append(" limit ");
		sqlBuilder.append(pagingBounds.getOffset());
		sqlBuilder.append(",");
		sqlBuilder.append(pagingBounds.getLimit());
		System.out.println(sqlBuilder.toString());
		return sqlBuilder.toString();
	}

}
